<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;
// Knowledgeright manager
class ViewknowledgenodeController extends BaseController {
    // get knowledge node msg
    public function  GetTypeMsg(){
        $res = array('rcode'=>0,'data'=>'','msg'=>'');
		$knowledge_id = I('post.knowledgeId',false,'intval');
		if($knowledge_id){
			$callfun = M('knowledge')	
									->alias("kl")
									->field('kl.*,a.nickname,b.sector_name')
									->join('LEFT JOIN  user as a ON a.user_id = kl.author_id')
									->join('LEFT JOIN  sector as b ON b.sector_id = a.sector_id')
									->where(array('kl.knowledge_id'=>$knowledge_id,'kl.is_delete'=>array('neq',1)))
									->find();

			$callfunfile = M('knowledge_file')->where(array('knowledge_id'=>$knowledge_id,'is_delete'=>array('neq',1)))->select();
		}
		if($callfun){
			$res['rcode'] = 1;
			$res['data'] = $callfun;
			if($callfunfile){
				$res['callfun'] = $callfunfile;
			}
		}else{
			// 
		}
		e_json($res);
    }

	// show the path of knowledge node
	public function ShowNode(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$knowledge_spot_id = I('post.knowledge_spot_id',false,'intval');
		$user_id = $this->m_user['user_id'];
		$sector_id = $this->m_user['sector_id'];
		$this->m_is_admin ? $right = 1 : $right = 0;
		$code = GetNodePath($knowledge_spot_id,$user_id,$sector_id,$right);
		if($code){
			$res['rcode'] = 1;
			$res['data'] = $code;
		}
		e_json($res);
	}

	// Judge User Right
	public function JudgeUserRight(){
        $res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$arr = $sec = $user = array();
		$knowledge_spot_id = I('knowledge_spot_id',false,'intval');
		if($this->m_user['user_id'] == 1){
			$res['rcode'] = 1;
			$res['exsit_right'] = 1;
		}else{
			$data = M('sector')->select();
			$callfunsec = GetParentSector($data,$this->m_user['sector_id']);
			$callfunall = M('knowledge_spot_sector_right')->distinct(true)->field('knowledge_spot_id')->where(array('sector_id'=>array('IN',$callfunsec),'knowledge_spot_id'=>$knowledge_spot_id))->count();
			if($callfunall){
				$res['rcode'] = 1;
				$res['exsit_right'] = 1;
			}else{
				$callfunuser = M('knowledge_spot_user_right')->distinct(true)->field('knowledge_spot_id')->where(array('user_id'=>$this->m_user['user_id'],'knowledge_spot_id'=>$knowledge_spot_id))->count();
				if($callfunuser){
					$res['rcode'] = 1; 
					$res['exsit_right'] = 1;
				}else{
					$res['rcode'] = 1;
					$res['exsit_right'] = 2;
				}				
			}
		}
        e_json($res);		
	}

	// download second tab file
	public function DownSecondTabFile(){
		$res = array('rcode'=>0,'data'=>'','msg'=>'');
		$knowledge_file_id = I('get.knowledgefileId',false,'intval');
		DownLoadFile($knowledge_file_id);
	}

	// get file name and extension name
	public function GetFileName(){
		$res = array('rcode'=>0,'data'=>'','msg'=>'');
		$knowledge_file_id = I('get.knowledgefileId',false,'intval');
		$callfun = GetFName($knowledge_file_id);
		if($callfun){
			$res['rcode'] = 1;
			$res['extersion'] = $callfun['extersion'];
			$res['name'] =$callfun['name'];			
		}
		e_json($res);
	}

}?>